import { GetShowVerificationStore } from '@/service/login';
import { Action, createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
import { HYDRATE } from 'next-redux-wrapper';

export interface HomeInitialState {
	num: number;
	showVerification: string;
	listData?: {
		num: number;
		showVerification: string;
	};
}

export const fetchListData = createAsyncThunk(
	'home/fetchListData',
	async (_, { getState }) => {
		const state = getState();
		console.log('state', state);
		return ;
	}
);

console.log('fetchListData', fetchListData());

const homeSlice = createSlice({
	name: 'home',
	initialState: {
		num: 0,
		showVerification: '关闭'
	} as HomeInitialState,
	reducers: {
		increment: (state, action: PayloadAction<number>) => {
			state.num += action.payload;
		},
		decrement: (state, action: PayloadAction<number>) => {
			state.num -= action.payload;
		}
	},
	extraReducers: (builder) => {
		builder
			.addCase(HYDRATE, (state: HomeInitialState, action: any) => {
				return {
					...state,
					...action.payload.home
				};
			})
			.addCase(fetchGetShowVerification.fulfilled, (state, { payload }) => {
				state.showVerification = payload;
			})
			.addCase(fetchListData.fulfilled, (state, { payload }) => {
				state.listData = payload;
			});
	}
});

export const fetchGetShowVerification = createAsyncThunk('fetchGetShowVerification', async (params: { channel: string }) => {
	const res = await GetShowVerificationStore(params);
	console.log('res', res);
	return res;
});

export const { increment, decrement } = homeSlice.actions;
export default homeSlice.reducer;
